/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "Boid.h"

#include "SceneObject.h"
#include "BoidController.h"

namespace PQClient
{
	const Ogre::Real Boid::FISH_SCALE=4;

	Boid::Boid(PQEngine::SceneObject* scene, Ogre::String unitName,Ogre::String fileName)
		: Unit(unitName,scene,Unit_Boid),_show(false),_fileName(fileName)
	{

		
		_rayQuery=_sceneObject->createRayQuery(Ogre::Ray());

		loadMesh();
		
		_controller=new BoidController(this);

		this->setVisible(true);
	}

	void Boid::loadMesh()
	{
		
		//we now hard code,will use config file
		_bodyMeshObj= _sceneObject->loadMeshObject(_fileName);
		_bodyMeshObj->getMeshComponent()->setAnim("swim");
		_bodyMeshObj->getMeshComponent()->enableAnim(true);

		_bodyMeshObj->setScale(Ogre::Vector3::UNIT_SCALE * FISH_SCALE);

		//MeshObject* meshObj=boid->getMeshObject();
		//_bodyMeshObj->setScale(Ogre::Vector3::UNIT_SCALE * 4);

		//MeshObject* obj=_boids[i]->getMeshObject();
		// set the new position based on the spline path and set the direction based on displacement
		//Ogre::Vector3 lastPos = obj->getPosition();
		//_bodyMeshObj->setPosition(612.902,500.419,1524.02);


		


	}

	void Boid::setVisible(bool show)
	{
		_show=show;
		_bodyMeshObj->getMeshComponent()->setVisible(show);
		
	}

	void Boid::update(float deltaT)
	{
		//_controller->update(deltaT);
		keepOnGround();
		_bodyMeshObj->update(deltaT);

	}

	void Boid::keepOnGround()
	{
		/*
		MeshObject* obj=_bodyMeshObj;

		_rayQuery->setRay(Ogre::Ray(obj->getPosition()+Ogre::Vector3(0,-2000,0),Ogre::Vector3::UNIT_Y));

		Ogre::RaySceneQueryResult &result=_rayQuery->execute();
		Ogre::RaySceneQueryResult::iterator it=result.begin();
		if(it!=result.end()&&it->worldFragment){
			obj->setPosition(obj->getPosition().x,it->worldFragment->singleIntersection.y+10,obj->getPosition().z);
		}
		*/


	}

	MeshObject* Boid::getMeshObject()
	{
		return _bodyMeshObj;
	}

}
